//
//  ViewController.m
//  ReadFileDemo
//
//  Created by CRY on 16/7/26.
//  Copyright © 2016年 EGOVA. All rights reserved.
//

#import "ViewController.h"
#import "ReadFile.h"

@interface ViewController()
@property (weak) IBOutlet NSTextField *inputTextField;
@property (weak) IBOutlet NSTextField *outputTextField;

@property (weak) IBOutlet NSButton *typeH;
@property (weak) IBOutlet NSButton *typeM;
@property (weak) IBOutlet NSButton *typeXib;
@property (weak) IBOutlet NSButton *typeJpg;
@property (weak) IBOutlet NSButton *typePng;

@property (nonatomic, strong) NSOpenPanel    *panel;
@property (nonatomic, strong) NSArray        *buttonArray;
@property (nonatomic, strong) NSMutableArray *selectItems;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _buttonArray = @[@"h",@"m",@"xib",@"jpg",@"png"];
    _selectItems = [NSMutableArray array];
}

- (void)setRepresentedObject:(id)representedObject {
    [super setRepresentedObject:representedObject];

    // Update the view, if already loaded.
}

- (IBAction)onClickFindInputPathButton:(id)sender {
    
    NSString *path_all;
    NSInteger result = [self.panel runModal];
    if (result == NSFileHandlingPanelOKButton){
        path_all = [[self.panel URL] path];
        _inputTextField.stringValue = path_all;
    }
}

- (IBAction)onClickFindOutputPathButton:(id)sender {
    NSString *path_all;
    NSInteger result = [self.panel runModal];
    if (result == NSFileHandlingPanelOKButton){
        path_all = [[self.panel URL] path];
        _outputTextField.stringValue = path_all;
    }
}

- (IBAction)onClickConfirm:(id)sender {
    
    [_selectItems removeAllObjects];
    for (int i = 100; i <= 104; i++) {
        NSButton *button = [self.view viewWithTag:i];
        if (button.state == 1) {
            [_selectItems addObject:_buttonArray[i-100]];
        }
    }
    [ReadFile readFileWithPath:_inputTextField.stringValue
                     fileTypes:_selectItems
                    outputPath:_outputTextField.stringValue];
}

- (IBAction)onClickReset:(id)sender {
    
    _inputTextField.stringValue  = @"";
    _outputTextField.stringValue = @"";
    for (int i = 100; i <= 104; i++) {
        NSButton *button = [self.view viewWithTag:i];
        button.state = 0;
    }
}

- (NSOpenPanel *)panel{
    
    if (_panel == nil) {
        _panel = [NSOpenPanel openPanel];
        [_panel setMessage:@""];
        [_panel setPrompt:@"OK"];
        [_panel setCanChooseDirectories:YES];
        [_panel setCanCreateDirectories:YES];
        [_panel setCanChooseFiles:YES];
    }
    return _panel;
}

@end
